<template>
  <div>
    <el-container class="header">
      <!-- 头 -->
      <el-header class="father">
        <!-- 搜索栏 -->
        <el-form :model="form" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="标签名称">
                <el-input v-model="form.tags" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市">
                <el-select
                  v-model="form.province"
                  @keyup.enter="handleFilter"
                  @change="handleProvince"
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in citySelect.province"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="地区">
                <el-select
                  v-model="form.city"
                  @keyup.enter="handleFilter"
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in citySelect.cityDate"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input v-model="form.shortName"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="7">
              <el-form-item label="状态">
                <el-select v-model="form.state" placeholder="请选择">
                  <el-option label="启用" :value="1"></el-option>
                  <el-option label="停用" :value="0"></el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="14">
              <div>
                <el-button class="confirm">清空</el-button>
                <el-button type="primary" class="cancel" @click="onSubmit"
                  >搜索</el-button
                >
              </div></el-col
            >
            <el-col :span="1"
              ><el-button
                class="added"
                type="success"
                icon="el-icon-edit"
                @click="openDrawer('add')"
                >新增用户</el-button
              ></el-col
            >
          </el-row>
        </el-form>

        <!-- 新增用户 -->
        <CompanysAdd
          :titleInfo="titleInfo"
          :formBase="formBase"
          :flag.sync="flag"
          @newDataes="newDataes"
        ></CompanysAdd>
      </el-header>

      <!-- 身体 -->
      <el-main class="tbody">
        <!-- 数据条数 -->
        <div class="recording">
          <span
            ><i class="el-icon-info"> </i> 共
            {{ tableData.counts }} 条记录</span
          >
        </div>

        <!-- table表格数据区域 -->
        <el-table :data="list" style="width: 100%" stripe v-loading="loading">
          <el-table-column prop="id" label="序号" align="center">
          </el-table-column>
          <el-table-column prop="number" label="企业编号" align="center">
          </el-table-column>
          <el-table-column prop="shortName" label="企业简称" align="center">
          </el-table-column>
          <el-table-column prop="tags" label="标签" align="center">
          </el-table-column>
          <el-table-column prop="creatorID" label="创建者" align="center">
          </el-table-column>
          <el-table-column prop="addDate" label="创建日期" align="center">
          </el-table-column>
          <el-table-column prop="remarks" label="备注" align="center">
          </el-table-column>
          <el-table-column prop="state" label="状态" align="center">
          </el-table-column>
          <el-table-column label="操作" width="120" align="center">
            <!-- 表格子项的作用域插槽 -->
            <template #default="{ row }">
              <div>
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  plain
                  circle
                  @click="openDrawer('edit', row.id)"
                ></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  plain
                  circle
                  @click="del(row.id)"
                ></el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件区域 -->
        <el-row type="flex" style="height: 60px" align="middle" justify="end">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryObj.page"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="queryObj.pagesize"
            layout="prev, pager, next, sizes, jumper"
            :total="tableData.counts"
          >
          </el-pagination>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import CompanysAdd from '../components/companys-add.vue'
import { list, detail, remove } from '@/api/hmmm/companys.js'
import { provinces, citys } from '@/api/hmmm/citys.js'

export default {
  components: {
    CompanysAdd
  },
  data () {
    return {
      flag: false,
      tableData: {},
      list: [],
      queryObj: {
        page: 1, // 控制当前页
        pagesize: 10 // 控制每页有几条数据
      },
      loading: false,
      titleInfo: {
        text: '创建',
        pageTitle: '用户'
      },
      formBase: {
        id: null,
        shortName: '',
        city: '',
        remarks: '',
        tags: '',
        province: '',
        company: '',
        isFamous: ''
      },
      form: {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: ''
      },
      citySelect: {
        province: '',
        cityDate: ''
      }
    }
  },
  created () {
    this.initTable()
    this.getCityData()
  },
  methods: {
    // 渲染
    async initTable () {
      this.loading = true
      const { data } = await list(this.queryObj)
      this.tableData = data
      data.items.forEach((item) => {
        if (item.state === 1) {
          item.state = '启用'
        } else {
          item.state = '禁用'
        }
      })
      this.list = data.items
      this.loading = false
    },
    // 分页功能
    // pagesize改变时会触发
    handleSizeChange (val) {
      this.queryObj.pagesize = val
      this.queryObj.page = 1
      this.initTable()
    },
    // page当前页改变时会触发
    async handleCurrentChange (val) {
      // 改变参数对象中的当前页
      this.queryObj.page = val
      this.initTable()
    },
    // 搜索
    onSubmit () {
      list(this.form)
      this.form = {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: ''
      }
    },
    // 获取省
    getCityData: function () {
      this.citySelect.province = provinces()
    },
    // 选省获取到市
    handleProvince: function (e) {
      this.citySelect.cityDate = citys(e)
    },
    // 删除功能
    async del (id) {
      try {
        await remove({ id })
        this.$message.success('删除成功')
        this.initTable()
      } catch (error) {
        this.$message.error('删除失败')
      }
    },
    // 新增
    async openDrawer (type, id) {
      if (type === 'add') {
        this.flag = true
      } else {
        this.titleInfo.text = '编辑'
        this.flag = true
        this.formBase.id = id
        const { data } = await detail({ id })
        this.formBase = data
      }
    },
    // 清空表单
    newDataes () {
      this.flag = false
      this.formBase = {
        id: null,
        shortName: '',
        city: '',
        remarks: '',
        tags: '',
        province: '',
        company: '',
        isFamous: ''
      }
    }
  }
}
</script>

<style scoped lang="less">
.header {
  background-color: #fff;
  margin: 10px 20px;
  border-radius: 5px;
  box-shadow: 0px 0px 20px #ccc;
  .father {
    padding: 20px 20px;
    margin-bottom: 60px;
    .added {
      height: 36px;
      width: 96px;
      font-size: 12px;
      padding-left: 11px;
    }
  }
}
.tbody {
  .recording {
    height: 35px;
    width: 100%;
    padding: 8px 16px;
    background-color: #f4f4f5;
    border-radius: 5px;
    span {
      font-size: 13px;
      color: #909399;
    }
  }
}
.confirm {
  height: 36px;
  width: 56px;
  padding-left: 15px;
  font-size: 12px;
}
.cancel {
  height: 36px;
  width: 56px;
  padding-left: 15px;
  font-size: 12px;
}
</style>
